home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / timing.c < prev   
C/C++ Source or Header  |  1997-07-22  |  5KB  |  203 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: timing.c,v 1.3 1997/07/09 13:26:18 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34. *    timing.c
  35. *
  36. *    Does a few communication timing tests on pvm.
  37. *    Uses `timing_slave' to echo messages.
  38. * ----------------------------------------------------------------------------
  39. *     If this test is run over machines with differnet data formats
  40. *     Then change 'ENCODING' to PvmDataDefault in timing and timing_slave
  41. * ----------------------------------------------------------------------------
  42. *
  43. *    9 Dec 1991  Manchek
  44. *  14 Oct 1992  Geist  - revision to pvm3
  45. *   6 Mar 1994  Geist  - synch tasks and add direct route
  46. */
  47.  
  48. #ifdef HASSTDLIB
  49. #include <stdlib.h>
  50. #endif
  51. #include <stdio.h>
  52.  
  53. #ifndef WIN32
  54. #include <sys/time.h>
  55. #endif
  56.  
  57. #include <time.h>
  58. #include <sys/types.h>
  59. #include <fcntl.h>
  60. #include <math.h>
  61. #include "pvm3.h"
  62.  
  63.  
  64. #define SLAVENAME "timing_slave"
  65. #define ENCODING  PvmDataRaw
  66.  
  67. main(argc, argv)
  68.     int argc;
  69.     char **argv;
  70. {
  71.     int mytid;                  /* my task id */
  72.     int stid = 0;                /* slave task id */
  73.     int reps = 20;                /* number of samples per test */
  74.     struct timeval tv1, tv2;    /* for timing */
  75.     int dt1, dt2;                /* time for one iter */
  76.     int at1, at2;                /* accum. time */
  77.     int numint;                    /* message length */
  78.     int n;
  79.     int i;
  80.     int *iarray = 0;
  81.  
  82.     /* enroll in pvm */
  83.  
  84.     if ((mytid = pvm_mytid()) < 0) {
  85.         exit(1);
  86.     }
  87.     printf("i'm t%x\n", mytid);
  88.  
  89.     /* start up slave task */
  90.  
  91.     if (pvm_spawn(SLAVENAME, (char**)0, 0, "", 1, &stid) < 0 || stid < 0) {
  92.         fputs("can't initiate slave\n", stderr);
  93.         goto bail;
  94.     }
  95.  
  96.     /* Wait for slave task to start up */
  97.     pvm_setopt(PvmRoute, PvmRouteDirect);
  98.     pvm_recv( stid, 0 );
  99.     printf("slave is task t%x\n", stid);
  100.  
  101.     /*
  102.     *  round-trip timing test
  103.     */
  104.  
  105.     puts("Doing Round Trip test, minimal message size\n");
  106.     at1 = 0;
  107.  
  108.     /* pack buffer */
  109.  
  110.     pvm_initsend(ENCODING);
  111.     pvm_pkint(&stid, 1, 1);
  112.  
  113.     puts(" N     uSec");
  114.     for (n = 1; n <= reps; n++) {
  115.         gettimeofday(&tv1, (struct timezone*)0);
  116.  
  117.         if (pvm_send(stid, 1)) {
  118.             fprintf(stderr, "can't send to \"%s\"\n", SLAVENAME);
  119.             goto bail;
  120.         }
  121.  
  122.         if (pvm_recv(-1, -1) < 0) {
  123.             fprintf(stderr, "recv error%d\n" );
  124.             goto bail;
  125.         }
  126.  
  127.         gettimeofday(&tv2, (struct timezone*)0);
  128.  
  129.         dt1 = (tv2.tv_sec - tv1.tv_sec) * 1000000 + tv2.tv_usec - tv1.tv_usec;
  130.         printf("%2d %8d\n", n, dt1);
  131.         at1 += dt1;
  132.     }
  133.     printf("RTT Avg uSec %d\n", at1 / reps);
  134.  
  135.     /*
  136.     *  bandwidth test for different message lengths
  137.     */
  138.  
  139.     puts("\nDoing Bandwidth tests\n");
  140.  
  141.     for (numint = 25; numint < 1000000; numint *= 10) {
  142.         printf("\nMessage size %d\n", numint * 4);
  143.         at1 = at2 = 0;
  144.         iarray = (int*)malloc(numint * sizeof(int));
  145.         puts(" N  Pack uSec  Send uSec");
  146.         for (n = 1; n <= reps; n++) {
  147.             gettimeofday(&tv1, (struct timezone*)0);
  148.  
  149.             pvm_initsend(ENCODING);
  150.             pvm_pkint(iarray, numint, 1);
  151.  
  152.             gettimeofday(&tv2, (struct timezone*)0);
  153.             dt1 = (tv2.tv_sec - tv1.tv_sec) * 1000000
  154.                 + tv2.tv_usec - tv1.tv_usec;
  155.  
  156.             gettimeofday(&tv1, (struct timezone*)0);
  157.  
  158.             if (pvm_send(stid, 1)) {
  159.                 fprintf(stderr, "can't send to \"%s\"\n", SLAVENAME);
  160.                 goto bail;
  161.             }
  162.  
  163.             if (pvm_recv(-1, -1) < 0) {
  164.                 fprintf(stderr, "recv error%d\n" );
  165.                 goto bail;
  166.             }
  167.  
  168.             gettimeofday(&tv2, (struct timezone*)0);
  169.             dt2 = (tv2.tv_sec - tv1.tv_sec) * 1000000
  170.                 + tv2.tv_usec - tv1.tv_usec;
  171.  
  172.             printf("%2d   %8d   %8d\n", n, dt1, dt2);
  173.             at1 += dt1;
  174.             at2 += dt2;
  175.         }
  176.  
  177.         if (!(at1 /= reps))
  178.             at1 = 1;
  179.         if (!(at2 /= reps))
  180.             at2 = 1;
  181.         puts("Avg uSec");
  182.         printf("     %8d   %8d\n", at1, at2);
  183.         puts("Avg Byte/uSec");
  184.         printf("     %8f   %8f\n",
  185.             (numint * 4) / (double)at1,
  186.             (numint * 4) / (double)at2);
  187.     }
  188.  
  189.     /* we have to do this because the last message might be taking
  190.     *  up all the shared memory pages.
  191.     */
  192.     pvm_freebuf(pvm_getsbuf());
  193.  
  194.     puts("\ndone");
  195.  
  196. bail:
  197.     if (stid > 0)
  198.         pvm_kill(stid);
  199.     pvm_exit();
  200.     exit(1);
  201. }
  202.  
  203.